package com.hjf.booking.security.config;

import com.hjf.booking.security.authentication.SelfUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author hjf
 * @date 2021-03-26 9:54
 */
@Component
public class TokenClearLogoutHandler implements LogoutHandler {

    @Autowired
    private SelfUserService userService;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        clearToken(authentication);
    }

    protected void clearToken(Authentication authentication) {
        if(authentication == null){
            return;
        }
        UserDetails user = (UserDetails)authentication.getPrincipal();
        if(user!=null && user.getUsername()!=null){
            userService.deleteUserLoginInfo(user.getUsername());
        }
    }

}
